package com.idp.web.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.test.dao.DeliverGoodsDao;
import com.idp.web.test.dao.OrdertDao;
import com.idp.web.test.entity.DeliverGoods;
import com.idp.web.test.entity.Logistics;
import com.idp.web.test.entity.Ordert;
import com.idp.web.test.entity.Warehouse;
import com.idp.web.test.service.DeliverGoodsService;
import com.idp.web.test.service.LogisticsHasWarehouseService;
import com.idp.web.test.service.LogisticsService;
import com.idp.web.test.service.OrdertService;
import com.idp.web.test.service.WarehouseService;

import net.sf.json.JSONObject;

/**
 * 
 * 1231232controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/deliverGoods")
public class DeliverGoodsController extends BaseController {

	private Logger logger = Logger.getLogger(DeliverGoodsController.class);

	@Resource
	private DeliverGoodsService deliverGoodsService;

	@Resource
	private OrdertService ordertService;

	@Resource
	private WarehouseService warehouseService;

	@Resource
	private LogisticsHasWarehouseService logisticsHasWarehouseService;

	@Resource
	private LogisticsService logisticsService;

	@Resource
	private DeliverGoodsDao deliverGoodsDao;

	@Resource
	private OrdertDao ordertDao;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init() {

		return "shopping/deliver/deliverGoodsSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param deliverGoods
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(DeliverGoods deliverGoods, Page<DeliverGoods> page, HttpServletRequest request) {

		try {

			List<Logistics> dlist = logisticsService.findBySearch(null);
			request.setAttribute("dlist", dlist);
			request.setAttribute("page", deliverGoodsService.findByPage(deliverGoods, page));
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/deliver/deliverGoodsList";
	}
	
	@RequestMapping("/listFrame")
	public String listFrame(DeliverGoods deliverGoods, Page<DeliverGoods> page, HttpServletRequest request) {

		try {

			List<Logistics> dlist = logisticsService.findBySearch(null);
			request.setAttribute("dlist", dlist);
			request.setAttribute("page", deliverGoodsService.findByPage(deliverGoods, page));
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/shipment/divflow";
	}
	
	
	@RequestMapping("/listFrameEdit")
	public String listFrameEdit(DeliverGoods deliverGoods, Page<DeliverGoods> page, HttpServletRequest request) {

		try {

			List<Logistics> dlist = logisticsService.findBySearch(null);
			request.setAttribute("dlist", dlist);
			request.setAttribute("page", deliverGoodsService.findByPage(deliverGoods, page));
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/shipment/divflowEdit";
	}


	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/deliverGoods")
	public String deliverGoods(Integer id, HttpServletRequest request) {
		System.out.println(request.getParameter("orderCode"));
		try {
			if (ValidateUtils.isNotEmpty(id)) {

				DeliverGoods deliverGoods = deliverGoodsService.getById(id);
				request.setAttribute("deliverGoods", deliverGoods);
			} else {
				Integer ordertId = deliverGoodsService.getIdByCode(request.getParameter("orderCode"));
				List<Logistics> loglist = logisticsService.findBySearch(null);

				request.setAttribute("warehouses", warehouseService.findBySearch(new Warehouse()));
				request.setAttribute("ordertId", ordertId);
				request.setAttribute("ordertCode", request.getParameter("orderCode"));
				request.setAttribute("loglist", loglist);
			}
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/deliver/deliverGoods";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param deliverGoods
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(DeliverGoods deliverGoods, HttpServletRequest request) {

		String orderid = request.getParameter("ordertId");
		JSONObject json = new JSONObject();

		System.out.println(deliverGoods.getLogisticsId());
		try {

			// 修改
			if (ValidateUtils.isNotEmpty(orderid)) {

				DeliverGoods deliverGoods2 = deliverGoodsDao.findByOrderid(Integer.parseInt(orderid));

				if (deliverGoods2 == null) {

					deliverGoods2 = new DeliverGoods();
					deliverGoods2.setOrdertId(Integer.parseInt(orderid));
					deliverGoods2.setLogisticsCode(deliverGoods.getLogisticsCode());
					deliverGoods2.setLogisticsId(deliverGoods.getLogisticsId());
					deliverGoodsDao.add(deliverGoods2);
				} else {

					deliverGoods2.setLogisticsCode(deliverGoods.getLogisticsCode());
					deliverGoods2.setLogisticsId(deliverGoods.getLogisticsId());
					deliverGoodsDao.updateNotNull(deliverGoods2);

				}

			}
			// 新增
			else {
				deliverGoodsService.add(deliverGoods);

			}

			Ordert ordert = ordertService.getById(Integer.parseInt(orderid));
			ordert.setOrderStatusId(2);
			ordertDao.updateNotNull(ordert);
			json.put("result", "save_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}

		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {

			deliverGoodsService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}

		return json.toString();
	}
}
